﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.ComponentModel;

namespace ClientAPI
{
    public class ClientAPI
    {
        private string chatLog;
        public delegate void ChatEventHandler(object sender, UpdateChatEventArgs eargs);
        public event ChatEventHandler updateChatHandler;
        private static ClientAPI _clientAPIInstance = null;
        bool _loggedIn = false;

        public static ClientAPI Instance
        {
            get
            {
                if (_clientAPIInstance == null)
                    _clientAPIInstance = new ClientAPI();
                return _clientAPIInstance;
            }
        }

        public ClientAPI()
        {
        }

        public bool IsConnected()
        {
            return Network.Instance.IsConnected();
        }

        public void SendChatMessage(string messageReceiver, string chatMessage)
        {
            Protocol.Instance.SendChat(messageReceiver, chatMessage);
            chatLog = Protocol.Instance.GetChatLog(messageReceiver);
            UpdateUI();
        }

        public void ChatMessageReceived(string messageSender)
        {
            chatLog = Protocol.Instance.GetChatLog(messageSender);
            UpdateUI();
        }

        public  void SendFile(string location, string filename, string fileInfo)
        {
            Protocol.Instance.SendFile(location, filename, fileInfo);
        }

        public bool Login(string user, string pass)
        {
            Protocol.Instance.Login(user, pass);
            _loggedIn = Protocol.Instance.WaitForLoginReply();
            return _loggedIn;
        }

        public void Connect(string domain)
        {
            Network.Instance.Connect(domain);
        }

        public bool Disconnect()
        {
            Network.Instance.Disconnect();
            return false;
        }

        public  void Create(string user, string first , string last , string pass)
        {
            Protocol.Instance.CreateUser(user, first, last, pass, "1800-blank", "blank@blank.com");
        }

        public  void Download(string fileName)
        {
            Protocol.Instance.Download(fileName);
        }

        public void UpdateUI()
        {
            if (updateChatHandler != null)
                updateChatHandler(this, new UpdateChatEventArgs());
        }

        public void SneakyHi()
        {
            Protocol.Instance.SneakyHi();
            UpdateUI();
        }

        public string GetChatLog(string userName)
        {
            string chatLog = Protocol.Instance.GetChatLog(userName);
            return chatLog;
        }
    }
   
    public class UpdateChatEventArgs
    {
        private string _chatLog;

        public string chatLog
        {
            get 
            {
                return _chatLog;
            }

            set
            {
                _chatLog = value;
            }
        }
    }
}

